﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using DaoService.Entity;
using DaoService.Model;
using DaoService.Services;
using UI.Models;

namespace UI.ViewModels;

public partial class PlcInfoVariablesViewModel : ObservableValidator
{

    private static PlcInfoVariablesService _plcInfoVariablesService = ServiceProvider.PlcInfoVariablesService;
    
    [ObservableProperty]
    private bool _isReadOnly;
    [ObservableProperty]
    private string _name;
    
    private ObservableCollection<PlcInfoVariable> _plcInfoVariables;

    public ObservableCollection<PlcInfoVariable> VariablesList
    {
        get
        {
            return _plcInfoVariables;   
        }
    }
    
    public ObservableCollection<PlcInfoVariable> PlcInfoVariables {
        get
        {
            loadPlcInfoVariables();
            return _plcInfoVariables;
        }
    }

    public async void loadPlcInfoVariables()
    {
        List<PlcInfoVariableEntity> list = await _plcInfoVariablesService.GetByNameAsync(Name);
        
        foreach (PlcInfoVariableEntity entity in list)
        {
            var model = PlcInfoVariableMapper.ToModelMapper.Map<PlcInfoVariable>(entity);
            _plcInfoVariables.Add(model);
        }
    }

    public async void EditPlcInfoVariable()
    {
        IsReadOnly = true;
    }
}